//package com.hitqz.robot.biz.flow.component.agv;
//
//import cn.hutool.json.JSONUtil;
//import com.baomidou.mybatisplus.core.toolkit.Wrappers;
//import com.hitqz.robot.api.business.entity.TicketsTaskEntity;
//import com.hitqz.robot.api.business.enums.RobotOtherCommandEnum;
//import com.hitqz.robot.api.business.enums.WorkStatusEnum;
//import com.hitqz.robot.api.business.model.dto.FlowPatrolPreAndEndTicketsDto;
//import com.hitqz.robot.api.common.dto.info.RobotNavInfoDto;
//import com.hitqz.robot.api.common.entity.Robot;
//import com.hitqz.robot.api.common.entity.RobotMapPos;
//import com.hitqz.robot.biz.business.dispatch.DispatchService;
//import com.hitqz.robot.biz.flow.util.LiteFlowUtil;
//import com.hitqz.robot.biz.service.RobotMapPosService;
//import com.hitqz.robot.biz.service.RobotService;
//import com.hitqz.robot.common.core.util.R;
//import com.hitqz.robot.driver.model.IRobotPosDto;
//import com.hitqz.robot.driver.parent.RobotDriverService;
//import com.yomahub.liteflow.annotation.LiteflowComponent;
//import com.yomahub.liteflow.core.NodeComponent;
//import lombok.AllArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.web.client.RestTemplate;
//
//import java.util.List;
//import java.util.Optional;
//import java.util.concurrent.TimeUnit;
//
///**
// * @author xupkun
// * @date 2024/12/3
// */
//
//@Slf4j
//@LiteflowComponent(value = "AgvPickPreCheck", name = "[轮对/轮轴]轮对AGV拿取前判断能不能取")
//@AllArgsConstructor
//public class LunduiAgvPickPreCheck extends NodeComponent {
//
//    private final RobotDriverService robotDriverService;
//
//    private final RobotMapPosService robotMapPosService;
//
//    private final RobotService robotService;
//
//    private final DispatchService dispatchService;
//
//    private final RestTemplate restTemplate;
//
//    private final String url = "http://127.0.0.1:9999/admin/agv/info";
//
//    @Override
//    public void process() throws Exception {
//        LiteFlowUtil.checkInterruptFlag();
//        IRobotPosDto iRobotPosDto = robotDriverService.getWebPos();
//        R currentResult = robotDriverService.executeOtherCommand(RobotOtherCommandEnum.JACKING_STATUS, null);
//        int currentJackStatus = (int) currentResult.getData();
//        if (iRobotPosDto.getFull() && currentJackStatus == 1) {
//            log.info("AGV带载，且顶升上升状态，不需要做判断");
//            return;
//        }
//
//        Robot robot = robotService.getDefaultRobot();
//        String response = restTemplate.getForObject(url, String.class);
//        List<RobotNavInfoDto> robotNavInfoList = JSONUtil.toList(JSONUtil.parseArray(response), RobotNavInfoDto.class);
//        RobotNavInfoDto currentNavInfo = robotNavInfoList.stream().filter(dto -> dto.getRobotSn()
//                .equals(robot.getRobotSn())).findFirst().orElse(null);
//        RobotNavInfoDto otherNavInfo = robotNavInfoList.stream().filter(dto -> !dto.getRobotSn()
//                .equals(robot.getRobotSn())).findFirst().orElse(null);
//        if (otherNavInfo == null) {
//            log.info("没有其他AGV,跳过！");
//            return;
//        }
//        //另一台AGV工作状态
//        String otherWorkStatus = otherNavInfo.getWorkStatus();
//        boolean otherIsFull = otherNavInfo.getIsFull();
//        String otherSourcePosCode = extractSourcePosCode(otherNavInfo);
//        String currentSourcePosCode = extractSourcePosCode(otherNavInfo);
//        //另一台AGV开始执行任务，且尚未拿到轮对,且拿轮对的地方和当前AGV相同
//        if (WorkStatusEnum.WORK.getCode().equals(otherWorkStatus)
//                && !otherIsFull
//                && otherSourcePosCode.equals(currentSourcePosCode)) {
//            Long otherTaskNo = Long.parseLong(otherNavInfo.getTaskNo());
//            Long currentTaskNo = Long.parseLong(currentNavInfo.getTaskNo());
//            if (otherTaskNo < currentTaskNo) {//另一台AGV需要先拿到轴，当前AGV去避让点等待
//                this.goToAvoidPos(currentSourcePosCode, robot.getMapCode());//当前AGV去合适的避让点等待
//                while (true) {
//                    response = restTemplate.getForObject(url, String.class);
//                    robotInfoList = JSONUtil.toList(JSONUtil.parseArray(response), AgvRobotInfoDto.class);
//                    otherRobotInfo = robotInfoList.stream().filter(dto -> !dto.getRobotNavInfoDto().getRobotSn()
//                            .equals(robot.getRobotSn())).findFirst().orElse(null);
//                    otherWorkStatus = otherRobotInfo.getRobotNavInfoDto().getWorkStatus();
//                    otherIsFull = otherRobotInfo.getRobotNavInfoDto().getIsFull();
//                    if (WorkStatusEnum.FREE.getCode().equals(otherWorkStatus) || otherIsFull) {
//                        log.info("检测到其他机器人{}已拿到轮对或已停止任务！当前AGV继续执行！", otherRobotInfo.getRobotNavInfoDto().getRobotSn());
//                        return;
//                    }
//                    TimeUnit.SECONDS.sleep(3);
//                }
//            }
//        }
//    }
//
//    private void goToAvoidPos(String currentSourcePosCode, String mapPosCode) {
//        String avoidPosCode = "";
//        switch (currentSourcePosCode) {
//            case "911007" -> avoidPosCode = "LM46";//北面取轮对
//            case "911008" -> avoidPosCode = "LM45";//南面取轮对
//        }
//        RobotMapPos robotMapPos = robotMapPosService.getOne(Wrappers.<RobotMapPos>lambdaQuery()
//                .eq(RobotMapPos::getCode, avoidPosCode)
//                .eq(RobotMapPos::getMapCode, mapPosCode));
//        boolean isSuccess = dispatchService.go2TargetPosition(robotMapPos, false);
//    }
//
//    private String extractSourcePosCode(RobotNavInfoDto dto) {
//        FlowPatrolPreAndEndTicketsDto parseOrderParams = JSONUtil.toBean(dto.getTaskParams(), FlowPatrolPreAndEndTicketsDto.class);
//        return String.valueOf(parseOrderParams.getModalData().get("sourcePosCode"));
//    }
//}
